#ifndef VIDEOWIDGET_H
#define VIDEOWIDGET_H

#include <QOpenGLWidget>
#include <QOpenGLFunctions>
#include <QTimer>

struct AVPacket;
struct AVFrame;
extern "C"
{
    #include <libavcodec/avcodec.h>
    #include <libavformat/avformat.h>
    #include <libswscale/swscale.h>
    #include <libavdevice/avdevice.h>
    #include <libavformat/version.h>
    #include <libavutil/time.h>
    #include <libavutil/mathematics.h>
    #include <libavutil/imgutils.h>
}
class VideoWidget : public QOpenGLWidget, protected QOpenGLFunctions
{
    Q_OBJECT
public:
    explicit VideoWidget(const QString &streamUrl, QWidget *parent = nullptr);
    ~VideoWidget();

protected:
    void initializeGL() override;
    void resizeGL(int w, int h) override;
    void paintGL() override;

private slots:
    void updateFrame();

private:
    void initFFmpeg(const QString &streamUrl);
    void decodeFrame();
    void convertFrameToRGB();

    AVFormatContext *pFormatCtx = nullptr;
    AVCodecContext *pCodecCtx = nullptr;
    AVFrame *pFrame = nullptr;
    AVFrame *pFrameRGB = nullptr;
    SwsContext *imgConvertCtx = nullptr;
    uint8_t *buffer = nullptr;
    int videoStreamIndex = -1;
    QTimer *timer = nullptr;

    GLuint textureID;
    AVPacket *packet = nullptr;
};

#endif // VIDEOWIDGET_H
